package com.lishe.lishe.lishe.module.auth.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lishe.lishe.R;
import com.lishe.lishe.asynchttp.LSWebHelper;
import com.lishe.lishe.lishe.module.auth.http.AuthHttpHelper;
import com.lishe.lishe.lishe.module.auth.view.SendCodeTextView;
import com.lishe.lishe.lishe.utils.LoadingUtils;
import com.lishe.lishe.lishe.utils.ValidationUtils;

import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by zhcase on 2017/9/5.
 */

public class ForgetPwdEndFragment extends AuthBaseFragment implements View.OnClickListener {

    public static final String TAG = ForgetPwdEndFragment.class.getSimpleName();

    ViewPager viewPager = null;

    private EditText inputText1, inputText2;

    private SendCodeTextView mInputText_sendCode;

    private Button submitBtn;


    public ForgetPwdEndFragment() {
    }

    public static ForgetPwdEndFragment newInstance(){
        ForgetPwdEndFragment instance = new ForgetPwdEndFragment();
        Bundle bundle = new Bundle();
        instance.setArguments(bundle);
        return instance;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View containerFragment = inflater.inflate(R.layout.activity_auth_forget_pwd_end, container, false);

        TextView nav_title_text = (TextView)containerFragment.findViewById(R.id.nav_title_text);
        nav_title_text.setText("忘记密码");

        Button back = (Button) containerFragment.findViewById(R.id.nav_left_button);
        back.setOnClickListener(this);

        inputText1 = (EditText) containerFragment.findViewById(R.id.inputText1);
        inputText1.setText("");
        inputText1.addTextChangedListener(watcher);

        inputText2 = (EditText) containerFragment.findViewById(R.id.inputText2);
        inputText2.setText("");
        inputText2.addTextChangedListener(watcher);

        submitBtn = (Button) containerFragment.findViewById(R.id.submitBtn);
        submitBtn.setOnClickListener(this);


        return containerFragment;
    }

    @Override
    public void onClick(View view) {

        if (view.getId() == R.id.nav_left_button) {
//            if (viewPager != null) {
//                viewPager.setCurrentItem(FragmentAuthContants.TAB_INDEX_Login, true);
//            }

            if(mJumper!=null)
            {
                mJumper.jumpToFragmentBack(FragmentAuthContants.TAB_INDEX_ForgetPwd);
            }




        } else if (view.getId() == R.id.redirectBtn1) {

            //用户协议
            if (viewPager != null) {
                //viewPager.setCurrentItem(FragmentAuthContants.TAB_INDEX_Login, true);
            }

        }  else if (view.getId() == R.id.submitBtn) {

            final String password1 = inputText1.getText().toString().trim();
            final String password2 = inputText2.getText().toString().trim();

            HashMap params = new HashMap();
            params.put("mobile",AuthHttpHelper.phoneOfForgetPwd);
            params.put("newPassword",password1);
            params.put("affirmPassword",password2);


            Log.e(TAG,AuthHttpHelper.phoneOfForgetPwd);


            LoadingUtils.createLoadingDialog(getActivity(),"请稍等...",true);
            AuthHttpHelper.doSetLoginPassword(getActivity(),params, new LSWebHelper.WebCallBack() {
                @Override
                public void onSuccess(JSONObject dataJson, String dataString, String message) {
                    AuthHttpHelper.phoneOfForgetPwd = null;
                    LoadingUtils.closeDialog();
                    inputText1.setText("");
                    inputText2.setText("");
                    if(mJumper!=null)
                        mJumper.jumpToFragmentBack(FragmentAuthContants.TAB_INDEX_Login);
                }

                @Override
                public void onFailure(String message) {
                    LoadingUtils.closeDialog();
                    Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
                }
            });


        }
    }

    private TextWatcher watcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            String input1 = inputText1.getText().toString();
            String input2 = inputText2.getText().toString();
            //Log.e(TAG,input1+"   "+input2);
            //Log.e(TAG,input1.length()+"   "+input2.length());

            if(!ValidationUtils.isLimitPass(input1)||!ValidationUtils.isLimitPass(input1)){
                Toast.makeText(getActivity(), R.string.LimitPass, Toast.LENGTH_SHORT).show();
                submitBtn.setEnabled(false);
                submitBtn.setBackgroundResource(R.drawable.shape_submit_pwd_change_bg_disable);
            }else
            {
                submitBtn.setEnabled(true);
                submitBtn.setBackgroundResource(R.drawable.shape_submit_pwd_change_bg);
            }

            if(input1.length()<6 || input2.length()<6 || !input1.equals(input2))
            {
                submitBtn.setEnabled(false);
                submitBtn.setBackgroundResource(R.drawable.shape_submit_pwd_change_bg_disable);
            }else
            {
                submitBtn.setEnabled(true);
                submitBtn.setBackgroundResource(R.drawable.shape_submit_pwd_change_bg);
            }

        }
    };

}
